﻿// =================================================================== 
// 项目说明
//====================================================================
// liucx @Copy Right 2019
// 文件： WashShopRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/11/18
// 负责人：liucx
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper;
using YouWei.Infrastructure.Models;
using Dapper.Extensions;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class WashShopRepository : BaseRepository<WashShop>
    {

        #region 构造

        public WashShopRepository() : base() { }

        public WashShopRepository(string connectionString) : base(connectionString) { }

        public WashShopRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion
        /// <summary>
        /// 查询产品列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<WashShop> Pages(int pageIndex, int pageSize, WashShopSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM [WashShop] T1 WITH(NOLOCK) WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.WashID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 查询洗车店
        /// </summary>
        /// <param name="districtId">区县ID</param>
        /// <param name="search">名称或地址</param>
        /// <returns></returns>
        public Page<WashShop> GetWorkShops(int districtId, string search, int pageIndex, int pageSize)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM [WashShop] T1 WITH(NOLOCK) WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (districtId > 0)
            {
                sb.Append(" AND T1.DistrictID=@DistrictID");
                dparams.Add("DistrictID", districtId, System.Data.DbType.Int32);
            }
            if (!string.IsNullOrEmpty(search))
            {
                sb.Append(" AND (WashName LIKE @Search OR Address LIKE @Search)");
                dparams.Add("Search", "%" + search + "%", System.Data.DbType.String);
            }
            sb.Append(" ORDER BY T1.WashID");

            //此处不能用分页查询
            Page<WashShop> pages = new Page<WashShop>();
            pages.rows = Query(sb.ToString(),dparams);
            pages.total = pages.rows.Count;
            return pages;
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(WashShopSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (model.WashID > 0)
            {
                sb.Append(" AND T1.WashID=@WashID");
                dparams.Add("WashID", model.WashID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.WashName))
            {
                sb.Append(" AND charindex(@WashName,T1.WashName)>0");
                dparams.Add("WashName", model.WashName, System.Data.DbType.String);
            }

        }
    }
}